package com.ruoyi.gateway.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @program: ruoyi
 * @description: 我的自定义过滤器
 * @author: yanghua
 * @create: 2020-09-07 19:58
 **/
@Component
public class MyTestFilter extends AbstractGatewayFilterFactory<Object> {
    private static final Logger log = LoggerFactory.getLogger(MyTestFilter.class);
    @Override
    public GatewayFilter apply(Object config) {
        log.info("我的自定义过滤器");
         return (exchange, chain) -> {
                //响应数据
             //ServerHttpResponse response = exchange.getResponse();

             ServerHttpRequest request = exchange.getRequest();
             RequestPath path = exchange.getRequest().getPath();
             log.info("path = "+path);
             //获取提交形式：post，get，delete，put
             log.info("exchange.getRequest().getMethod() = "+request.getMethod());
             log.info("exchange.getRequest().getMethodValue() = "+request.getMethodValue());

             log.info("exchange.getRequest().getHeaders() = "+request.getHeaders());
             HttpHeaders headers = request.getHeaders();
             //获取token
             log.info("获取token = "+headers.get("authorization"));
             //获取用户名username   MultiValueMap:这是一个key对应多个values的map集合 ---start
             MultiValueMap<String, HttpCookie> cookies = request.getCookies();
             Set<String> keySet1 = cookies.keySet();
             for (String key : keySet1) {
                 List<Object> values = Collections.singletonList(cookies.get(key));

                 for (int i = 0; i <values.size() ; i++) {
                     String s = values.get(i).toString();
                     if (s.contains("username")){
                         log.info("获取用户名username = "+s);
                     }

                 }

             }
             //获取用户名username   MultiValueMap:这是一个key对应多个values的map集合 ---end

             log.info("exchange.getRequest().getBody() = "+exchange.getRequest().getBody());

             MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
             Set<String> keySet = queryParams.keySet();
             for (String key : keySet) {
                 List<Object> values = Collections.singletonList(queryParams.get(key));
                 for (int i = 0; i <values.size() ; i++) {
                     log.info("get请求的入参 = "+values.get(i));
                 }
             }
             return chain.filter(exchange);
         };
    }
}
